/**
 * Created by Administrator on 2015/11/19.
 */
var conn = require('../util/conn');

var getUsers = function(page,rows,callback){

    var retObj = {};
    var connection = conn.getConnection();
    //connection.connect();
    if(!page || !rows){
        page = 1;
        rows = 10;
    }
    else{
        page = parseInt(page);
        rows = parseInt(rows);
    }
    var start = (page-1)*rows;
    connection.query('select count(*) as result from user',function(err,rows,fields){
        retObj.result = rows[0].result;//获取总行数
    });
    var query = connection.query('select * from user limit ?,?',[start,rows], function(err, rows, fields) {
        if (err) throw err;

        datas = [];
        for(var row in rows){
            var node = {
                id:rows[row].id,
                name:rows[row].name
            }
            datas[row] = node;
        }
        retObj.rows = datas;
        callback(retObj);
    });
    console.log(query.sql);
    connection.end();
}

var deleteUser = function(ids,callback){
    var connection = conn.getConnection();
    connection.connect();
    //connection.query('INSERT INTO user values(null,?,null)',)
    var idsStr = '';
    for(var i in ids){
        idsStr = idsStr +',' + ids[i];
    }
    idsStr = idsStr.replace(/(^,*)/g,'');
    idsStr = '( ' + idsStr + ' )';
    var query = connection.query('DELETE FROM user WHERE id in'+idsStr,function(err,result){
        if(err) throw  err;
        console.log('delete ' + result.affectedRows + ' rows');
        callback({affectedRows:result.affectedRows});
    });
    console.log(query.sql);
    connection.end();
}
/**
 *
 * @param user
 * @param callback
 */
var editUser = function(user,callback){
    var connection = conn.getConnection();
    connection.connect();
//    var query = connection.query('UPDATE user SET name = ?,departmentId = ? where id = ?',[user.name,user.departmentId,user.id],function(err,result){
    var query = connection.query('UPDATE user SET name = ? where id = ?',[user.name,user.id],function(err,result){
        if(err) throw err;
        console.log(result.changedRows);
        callback({changedRows:result.changedRows});
    });
    console.log(query.sql);
    connection.end();
}
/**
 * user
 * {
 *      id:null,
 *      name:'xx',
 *      departmentId:2
 * }
 * addUser(user,function(data){
 *      console.log(data);
 *      xx.send(data);
 * })
 * @param user
 */
var addUser = function(user,callback){
    //var retObj = {};
    var connection = conn.getConnection();
    connection.connect();
    var query = connection.query('INSERT INTO user SET ?',user,function(err,result){
        if (err) throw err;

        console.log(result.insertId);
        callback({insertId:result.insertId});
    });
    console.log(query.sql);
    connection.end();
}
/**
 *
 * TEST
 */
//function myoutput(data){
//    console.log(data);
//}
//getUsers(0,0,myoutput);
//deleteUser(6);
//addUser({id:null,
//         name:'xyxy',
//         departmentId:null
//        });
//editUser({id:10,
//         name:'yuzhou346694246',
//         departmentId:null
//        });
module.exports = {
    list:getUsers,
    add:addUser,
    edit:editUser,
    delete:deleteUser
};